<?php
/**
 * cms首页
 *
 *
 *
 
 */

// use Shopnc\Tpl;
defined('InShopNC') or exit('Access Invalid!');

class indexControl extends mobileHomeControl
{

    public function __construct()
    {
        parent::__construct();
    }

    
    /**
     * wap端首页接口整合
     */
    public function wapIndexOp()
    {
        // 广告轮播
        $model_mb_ad = Model('mb_ad');
        $ad_condition = array();
        if ($_POST['ad_code'] == null) {
            $ad_condition['ad_code'] = '0';
        }
        $ad_condition['ad_show_web'] = 1;
        $ad_data = $model_mb_ad->getMbAdList($ad_condition);
        $ad_num = $model_mb_ad->getMbAdCount($_POST['ad_code']);
        
        // 查找促销商品
        // $goods_condition['_op']='or';
        $goods_model = Model('goods');
        $promotion_condition['is_discount'] = 1; // 促销条件
                                                 // 促销
                                                 // $promotion_goods=$goods_model->getGoodsOnlineList($promotion_condition);
        $promotion_goods = $goods_model->getGoodsOnlineList($promotion_condition, $field = '*', $page = 0, $order = 'goods_id desc', 10);
        
        
        $goods_flag_model = Model('goods_flag');
        
        foreach ($promotion_goods as $key => $val) {
            // 切割图片路径
            $img = explode(".", $val['goods_image']); // 得到名称和后缀
            $promotion_goods[$key]['goods_image_0'] = UPLOAD_SITE_URL . '/shop/store/goods/1/' . $img[0] . '_60.' . $img[1];
            $promotion_goods[$key]['goods_image_1'] = UPLOAD_SITE_URL . '/shop/store/goods/1/' . $img[0] . '_240.' . $img[1];
            $promotion_goods[$key]['goods_image_2'] = UPLOAD_SITE_URL . '/shop/store/goods/1/' . $img[0] . '_360.' . $img[1];
            if($val['goods_discount_image'] !=null && !empty($val['goods_discount_image'])){
                $promotion_goods[$key]['goods_image_3'] = UPLOAD_SITE_URL . '/shop/store/goods/1/' . $val['goods_discount_image'];  
            }else{
                $promotion_goods[$key]['goods_image_3'] = UPLOAD_SITE_URL . '/shop/store/goods/1/' . $val['goods_image'];
                
            }
            if ($val['goods_flag'] != null && ! empty($val['goods_flag'])) {
                $con['flag_id'] = $val['goods_flag'];
                $flag = $goods_flag_model->get_goods_flag($con, 1);
                if ($flag != null && ! empty($flag)) {
                    $promotion_goods[$key]['goods_flag'] = $flag[0]['flag_name'];
                } else {
                    $promotion_goods[$key]['goods_flag'] = "促销";
                }
            } else {
                $promotion_goods[$key]['goods_flag'] = "促销";
            }
        }
        
        // 数组下标重新排序
        $promotion_goods = array_values($promotion_goods);
        
        
        $goods_list = $goods_model->getGoodsOnlineList(array(), '*', 0, 'goods_id desc', 10, null, false);
        foreach ($goods_list as $key => $val) {
            // 查找common表的主图
            $common['goods_commonid'] = $val['goods_commonid'];
            //$model_goods = Model('goods');
            $common_info = $goods_model->getGoodeCommonInfo($common);
            // 切割图片路径
            $img = explode(".", $val['goods_image']); // 得到名称和后缀
            $goods_list[$key]['goods_image'] = UPLOAD_SITE_URL . '/shop/store/goods/1/' . $common_info['goods_image'];
        }
        
        //首页分类
        $model_wp_class = Model('wp_class');
        $class_condition['class_loc'] = 0;
        $class_link_list = $model_wp_class->getWpClassList($class_condition);
        
        //今日快报
        $news_condition['class_loc'] = 1;
        $news_link_list = $model_wp_class->getWpClassList($news_condition);
        
        //促销
        $promotion_condition['class_loc'] = 2;
        $promotion_link_list = $model_wp_class->getWpClassList($promotion_condition);
        
        //推荐位
        $recommend_condition['class_loc'] = 3;
        $recommend_link_list = $model_wp_class->getWpClassList($recommend_condition);
        $recommend_link = $recommend_link_list[0];
        
        //滚动广告
        $swiper_condition['class_loc'] = 4;
        $swiper_link_list = $model_wp_class->getWpClassList($swiper_condition);
        
        $commend_condition['goods_commend'] = '1'; // 推荐条件
        $commend_condition['goods_commend_toIndex'] = '1'; // 推荐置顶首页
        // 推荐
        $commend_goods = $goods_model->getGoodsOnlineList($commend_condition, $field = '*', $page = 0, $order = 'goods_id desc', $limit = 3);
        foreach ($commend_goods as $key => $val) {
            // 切割图片路径
            $img = explode(".", $val['goods_image']); // 得到名称和后缀
            $commend_goods[$key]['goods_image_0'] = UPLOAD_SITE_URL . '/shop/store/goods/1/' . $img[0] . '_60.' . $img[1];
            $commend_goods[$key]['goods_image_1'] = UPLOAD_SITE_URL . '/shop/store/goods/1/' . $img[0] . '_240.' . $img[1];
            $commend_goods[$key]['goods_image_2'] = UPLOAD_SITE_URL . '/shop/store/goods/1/' . $img[0] . '_360.' . $img[1];
           /*  if($val['goods_commend_image'] !=null && !empty($val['goods_commend_image'])){
                $commend_goods[$key]['goods_image_3'] = UPLOAD_SITE_URL . '/shop/store/goods/1/' . $val['goods_commend_image'];
            }else{
                $commend_goods[$key]['goods_image_3'] = UPLOAD_SITE_URL . '/shop/store/goods/1/' . $val['goods_image'];
            } */
            $commend_goods[$key]['goods_image_3'] = UPLOAD_SITE_URL . '/shop/store/goods/1/' . $val['goods_image'];
        
            if ($val['goods_flag'] != null && ! empty($val['goods_flag'])) {
                $con['flag_id'] = $val['goods_flag'];
                $flag = $goods_flag_model->get_goods_flag($con, 1);
        
                if ($flag != null && ! empty($flag)) {
                    $commend_goods[$key]['goods_flag'] = $flag[0]['flag_name'];
                } else {
                    $commend_goods[$key]['goods_flag'] = "促销";
                }
            } else {
                $commend_goods[$key]['goods_flag'] = "促销";
            }
        }
        $recommend_link['commend_goods'] = $commend_goods;
        
        output_data(array(
            'ad_num' => $ad_num,
            'ad_list' => $ad_data,
            'promotion_goods' => $promotion_goods,
            'commend_goods' => $commend_goods,
            'goods_list' => $goods_list,
            
            'class_link_list' => $class_link_list,
            'news_link_list' => $news_link_list,
            'promotion_link_list' => $promotion_link_list,
            'swiper_link_list' => $swiper_link_list,
            'recommend_link' => $recommend_link
        ));
    }

    /**
     * 首页
     */
    public function indexOp()
    {
        $model_mb_special = Model('mb_special');
        $data = $model_mb_special->getMbSpecialIndex();
        $this->_output_special($data, $_GET['type']);
    }
    
    /**
     * 保存用户搜索的关键词
     */
    public function saveKeyOp(){
        $model_mb_user_token = Model('mb_user_token');
        $key=$_REQUEST['key'];
        $mb_user_token_info = $model_mb_user_token->getMbUserTokenInfoByToken($key);
        $model_member = Model('member');
        $member_info = $model_member->getMemberInfoByID($mb_user_token_info['member_id']);
        $data['key_word']=$_POST['key_word'];
        $data['key_mobile']=$member_info['member_mobile'];
        $data['key_name']=$member_info['member_name'];
        $data['key_ip']=$_SERVER["REMOTE_ADDR"];
        $data['key_time']=TIMESTAMP;
        $data['client']=$_POST['client'];
        $model_key=Model('key');
        $result=$model_key->saveKey($data);
        if($result){
            output_data(array('state'=>'success'));
        }else{
            output_data(array('state'=>'error'));
        }
    }
    
    /**
     * 保存用户浏览商品的操作
     */
    public function saveFeverOp(){
        $key=$_REQUEST['key'];
        $model_mb_user_token = Model('mb_user_token');
        $mb_user_token_info = $model_mb_user_token->getMbUserTokenInfoByToken($key);
        $model_member = Model('member');
        $member_info = $model_member->getMemberInfoByID($mb_user_token_info['member_id']);
        $data['fever_goodsid']=$_POST['fever_goodsid'];
        $data['fever_goodsname']=$_POST['fever_goodsname'];
        $data['fever_goodsprice']=$_POST['fever_goodsprice'];
        $data['fever_actid']=$_POST['fever_salesid'];
        $data['fever_acttype']=$_POST['fever_salestype'];
        $data['fever_actname']=$_POST['fever_salesname'];
        $data['fever_actiontype']=$_POST['fever_actiontype'];
        $data['fever_actiontime']=TIMESTAMP;
        $data['client']=$_POST['client'];
        $data['fever_actionmobile']=$member_info['member_mobile'];
        $model_fever=Model('fever');
        $result=$model_fever->saveFever($data);
        if($result){
            output_data(array('state'=>'success'));
        }else{
            output_data(array('state'=>'error'));
        }
    }
 

    /**
     * 保存WiFi平台礼品数据
     */
    public function saveGiftMessageOp()
    {
	    $content= file_get_contents("php://input");        
        $content=(json_decode($content,true));
	    $msg=str_replace("'","",$content );
	    $msg=$content['gifts'];
        $model_member = Model('member');       
        if ($msg!=null && !empty($msg) && is_array($msg)){
            $flag = true;
            foreach ($msg as $key => $val) {
                $member['member_mobile'] = $val['userCode'];
                $member_info = $model_member->getMemberInfo($member);
                $data['msg_receive_id'] = $member_info['member_id'];
                $data['msg_receive_name'] = $member_info['member_mobile'];
                if ($member_info['member_truename'] != null || ! empty($member_info['member_truename'])) {
                    $name = $member_info['member_truename'];
                } else {
                    $name = substr_replace($val['userCode'], "****", 3, 4); // 手机号
                }
                $content = "亲爱的" . $name . "，恭喜您在《" . $val['titleName'] . "》获得礼品《" . $val['giftName'] . "》一份，兑换截止时间为" . $val['exchangeDate'] . "，请您留意查收。可在我的礼品页面查看详情。";
                $data['msg_content'] = $content;
                $data['msg_time'] = TIMESTAMP;
                $data['msg_type'] = 4;
                $data['msg_state'] = 0;
                $data['remark'] = "礼品领取提醒";
                $model_message_center = Model('message_center');
                $result = $model_message_center->saveMsg($data);
                if (! $result) {
                    output_data(array(
                        'state' => 'err',
                        'msg' => '保存失败'
                    ));
                }
            }
            if ($flag) {
                output_data(array(
                    'state' => 'ok',
                    'msg' => '保存成功'
                ));
            }
        }else{
            output_data(array(
                'state' => 'err',
                'msg' => '数据接收失败'
            ));
        }
        
        
        
    }

    /**
     * 广告列表
     */
    public function getAdListOp()
    {
        $model_mb_ad = Model('mb_ad');
        $condition = array();
        if ($_POST['ad_code'] != null) {
            $condition['ad_code'] = $_POST['ad_code'];
        }
        $data = $model_mb_ad->getMbAdList($condition);
        $count = $model_mb_ad->getMbAdCount($_POST['ad_code']);
        output_data(array(
            'ad_num' => $count,
            'ad_list' => $data
        ));
    }

    /**
     * 首页推荐列表
     */
    public function getHadListOp()
    {
        $model_mb_homead = Model('mb_homead');
        $condition = array();
        if ($_POST['had_code'] != null) {
            $condition['had_code'] = $_POST['had_code'];
        }
        $data = $model_mb_homead->getMbHadList($condition);
        $count = $model_mb_homead->getMbHadCount($_POST['had_code']);
        output_data(array(
            'had_num' => $count,
            'had_list' => $data
        ));
    }

    /**
     * 专区推荐列表
     */
    public function getSpadListOp()
    {
        $model_mb_ad = Model('mb_specialad');
        $condition = array();
        if ($_REQUEST['spad_code'] != null) {
            $condition['spad_code'] = $_REQUEST['spad_code'];
        }
        
        $data = $model_mb_ad->getMbSpadList($condition);
        
        $count = $model_mb_ad->getMbSpadCount($_POST['spad_code']);
        output_data(array(
            'spad_num' => $count,
            'spad_list' => $data
        ));
    }

    /**
     * 首页模块分类列表
     */
    public function getClassListOp()
    {
        $model_mb_class = Model('mb_class');
        $condition = array();
        $data = $model_mb_class->getMbClassList($condition);
        $count = $model_mb_class->getMbClassCount();
        output_data(array(
            'class_num' => $count,
            'class_list' => $data
        ));
    }

    public function getACListOp()
    {
        $model_mb_ad = Model('mb_ad');
        $model_mb_class = Model('mb_class');
        $condition = array();
        if (! empty($_POST['ad_code'])) {
            $condition['ad_code'] = $_POST['ad_code'];
        }
        $data = $model_mb_ad->getMbAdList($condition);
        $count = $model_mb_ad->getMbAdCount();
        
        $data_class = $model_mb_class->getMbClassList($condition);
        $count_class = $model_mb_class->getMbClassCount();
        
        output_data(array(
            'ad_num' => $count,
            'ad_list' => $data,
            'class_num' => $count_class,
            'class_list' => $data_class
        ));
    }

    /**
     * 专题
     */
    public function specialOp()
    {
        $model_mb_special = Model('mb_special');
        $data = $model_mb_special->getMbSpecialItemUsableListByID($_GET['special_id']);
        $this->_output_special($data, $_GET['type'], $_GET['special_id']);
    }

    /**
     * 输出专题
     */
    private function _output_special($data, $type = 'json', $special_id = 0)
    {
        $model_special = Model('mb_special');
        if ($_GET['type'] == 'html') {
            $html_path = $model_special->getMbSpecialHtmlPath($special_id);
            if (! is_file($html_path)) {
                ob_start();
                Tpl::output('list', $data);
                Tpl::showpage('mb_special');
                file_put_contents($html_path, ob_get_clean());
            }
            header('Location: ' . $model_special->getMbSpecialHtmlUrl($special_id));
            die();
        } else {
            if (empty($data)) {
                $data = array();
            }
            // $data['site_mobile_logo']=array(C('site_mobile_logo'));
            // print_r($data);
            output_data($data);
        }
    }

    /**
     * android客户端版本号
     */
    public function apk_versionOp()
    {
        $version = C('mobile_apk_version');
        $url = C('mobile_apk');
        if (empty($version)) {
            $version = '';
        }
        if (empty($url)) {
            $url = '';
        }
        
        output_data(array(
            'version' => $version,
            'url' => $url
        ));
    }
    
    /**
     * 热门搜索
     */
    public function hotSearchOp(){
        $model_search=Model('hotsearch');
        $search_list=$model_search->getSearchList($condition=array(),'sort asc',6);
        if ($search_list){
            output_data(array(
                'state' => 'success',
                'search_list' => $search_list
            ));
        }else{
            output_data(//hotsearch.model.php on line 18
                array(
                    'state'=>'error',
                    'msg'=>'暂无'
                )
                );
        }
        
    }
    
}
